<?php
/*
 * 车型库字段扩展类
 * @package    auto
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: f22b745b40202ed61b52bdb894139cfe597faf7e $
 */
defined('IN_IXDCW') or exit('Access Denied');
/**
 * Class AutoFields
 */
class AutoFieldModule extends BasicModule {

	/** @var  条目的ID */
	private $_itemid;

	/** @var  操作的表, 没有前缀 */
	private $_tb;

	/** @var  当前正在操作的表 */
	private $_otb;

	/** @var  数据库前缀 */
	private $_pre;

	/** @var string 字段存储的表 */
	private $_table;

	/**
	 * constructor
	 */
	function __construct() {
		parent::__construct();
		$this->_pre = $this->_db->getPre();
		$this->_table = Table::name(Table::NAME_AUTO_FIELDS);
	}

	/**
	 * 设置需要操作的表
	 * @param $tb
	 */
	public function setTb($tb) {
		$this->_tb = $tb;
		$this->_otb = $this->_pre .$tb;
	}

	/**
	 * 设置需要操作的条目
	 * @param $itemid
	 */
	public function setItemid($itemid) {
		$this->_itemid = $itemid;
	}
	/**
	 * 字段检测
	 * @param $post
	 * @return bool
	 */
	function pass($post) {
		global $DT_TIME;
		if(!is_array($post)) return false;
		if(!$post['name']) return $this->err('请填写字段');
		if(!preg_match("/^[a-z_0-9]+$/", $post['name'])) return $this->err('字段名只能为小写字母和数字和下划线的组合');
		if(!$post['title']) return $this->err('请填写字段名称');
		if(in_array($post['html'], array('select', 'radio', 'checkbox'))) {
			if(!$post['option_value']) return $this->err('请填写选项值');
			if(strpos($post['option_value'], '|') === false) return $this->err('请填写正确的选项值');
		}
		return true;
	}

	/**
	 * 字段设置
	 * @param $post
	 * @return mixed
	 */
	function set($post) {
		if(!in_array($post['html'], array('select', 'radio', 'checkbox'))) {
			$post['option_value'] = '';
		}
		$post['length'] = intval($post['length']);
		if($post['html'] == 'textarea') {
			if($post['type'] != 'varchar' && $post['type'] != 'text') $post['type'] = 'text';
		} else if($post['html'] == 'checkbox' || $post['html'] == 'thumb' || $post['html'] == 'file') {
			$post['type'] = 'varchar';
			$post['length'] = 255;
		} else if($post['html'] == 'editor') {
			$post['type'] = 'text';
		} else if($post['html'] == 'area') {
			$post['type'] = 'int';
			$post['length'] = 10;
		}
		return $post;
	}

	/**
	 * 获取一条
	 * @return array|void
	 */
	function getOne() {
		return $this->_db->getOne("SELECT * FROM {$this->_table} WHERE itemid='$this->_itemid'");
	}

	/**
	 * 获取列表
	 * @param string $condition
	 * @param string $order
	 * @return array
	 */
	function getList($condition = '', $order = 'listorder ASC,itemid ASC') {
		$num = $this->_db->count($this->_table, $condition);
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($num);
		$sql = "SELECT * FROM {$this->_table}";
		$sql .= $condition ? " WHERE {$condition}" : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$lists = $this->_db->getAll($sql);
		return $lists;
	}

	/**
	 * 添加
	 * @param $post
	 * @return mixed
	 */
	function add($post) {
		$post = $this->set($post);
		$length = 0;
		if($post['type'] == 'varchar') {
			$length = min($post['length'], 255);
		} else if($post['type'] == 'int') {
			$length = min($post['length'], 10);
		}
		$type = strtoupper($post['type']);
		if($length) $type .= "($length)";
		$name = '`'.$post['name'].'`';
		$this->_db->query("ALTER TABLE {$this->_otb} ADD $name $type NOT NULL");
		$sqlk = $sqlv = '';
		foreach($post as $k=>$v) {
			$sqlk .= ','.$k; $sqlv .= ",'$v'";
		}
		$sqlk = substr($sqlk, 1);
		$sqlv = substr($sqlv, 1);
		$this->_db->query("INSERT INTO {$this->_table} ($sqlk) VALUES ($sqlv)");
		return $this->_itemid;
	}

	/**
	 * 编辑
	 * @param $post
	 * @return bool
	 */
	function edit($post) {
		$post = $this->set($post);
		$length = 0;
		if($post['type'] == 'varchar') {
			$length = min($post['length'], 255);
		} else if($post['type'] == 'int') {
			$length = min($post['length'], 10);
		}
		$type = strtoupper($post['type']);
		if($length) $type .= "($length)";
		$cname = '`'.$post['cname'].'`';
		unset($post['cname']);
		$name = '`'.$post['name'].'`';
		$this->_db->query("ALTER TABLE {$this->_otb} CHANGE $cname $name $type NOT NULL");
		$sql = '';
		foreach($post as $k=>$v) {
			$sql .= ",$k='$v'";
		}
		$sql = substr($sql, 1);
		$this->_db->query("UPDATE {$this->_table} SET $sql WHERE itemid=$this->_itemid");
		return true;
	}

	/**
	 * 删除
	 * @param $itemid
	 */
	function delete($itemid) {
		$this->_itemid = $itemid;
		$r = $this->getOne();
		$name = '`'.$r['name'].'`';
		$this->_db->query("DELETE FROM {$this->_table} WHERE itemid=$itemid");
		$this->_db->query("ALTER TABLE {$this->_otb} DROP $name");
	}

	/**
	 * 设置排序
	 * @param $listorder
	 * @return bool
	 */
	function order($listorder) {
		if(!is_array($listorder)) return false;
		foreach($listorder as $k=>$v) {
			$k = intval($k);
			$v = intval($v);
			$this->_db->query("UPDATE {$this->_table} SET listorder=$v WHERE itemid=$k");
		}
		return true;
	}
}